#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
using namespace std;
int main()
{

    int arr[100010];
    int n;
    cin >> n;
    for (int i = 0;i < n;i++)
        cin >> arr[i];

    int ret = 0;
    for (int i = 1; i < n; i++)
    {
        if (arr[i] > arr[i - 1])
            ret += arr[i] - arr[i - 1];
    }

    cout << ret << endl;
    return 0;
}